<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Assets;
use app\common\model\Evaluate;
use app\common\model\OrderProduct;
use app\common\model\UserCoupon;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;
use think\response\Json;
use app\common\model\ActivityOrder as ActivityOrderModel;

class ActivityOrder extends Base
{

    /**
     * get: 订单信息
     * path: getTemporary
     * method: getTemporary
     */
    public function getTemporary()
    {
        try {
            $temporary = ActivityOrderModel::getTemporary($this->user->id);
            if($temporary['code']>0)
            {
                return Result::success('获取成功',$temporary['data']);
            }else{
                return Result::fail('获取失败',$temporary['msg']);
            }
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

    }



    /**
     * post: 提交订单
     * path: submit
     * method: submit
     */
    public function submit()
    {
        $param = $this->request->param();
        $submit = ActivityOrderModel::submit($this->user,$param);

        return Result::fastReturn($submit);
    }



    /**
     * post: 选择支付方式
     * path: payment
     * method: payment
     * param: a_id - {string} 支付订单id
     */
    public function payment()
    {
        $param = $this->request->post();
        if(isset($param['a_id']))
        {
            $info = OrderModel::find($param['p_id']);
            $result['total'] = $info->total_amount;
            $result['order_type'] = $info->order_type;
        }else{
            $info = OrderModel::getNowOrder($this->user->id);
            if ($info['code'] != 1) {
                return json(['code' => -1, 'msg' => '加载失败，未查询到待支付的订单']);
            }
            $result['total'] = $info['result']['total_amount'];
            $result['order_type'] = $info['result']['order_type'];
        }
        if($result['order_type']==1)
        {
            $result['method'] = [
                [
                    'title' => '余额',
                    'pay_way' => 'balance',
                    'icon' => handleImg('/static/index/img/wechat.png')
                ]
            ];
        }else{
            $result['method'] = [
                [
                    'title' => '微信',
                    'pay_way' => 'wechat',
                    'icon' => handleImg('/static/index/img/wechat.png')
                ],
                [
                    'title' => '佣金',
                    'pay_way' => 'integral',
                    'icon' => handleImg('/static/index/img/wechat.png')
                ],
                [
                    'title' => '余额',
                    'pay_way' => 'balance',
                    'icon' => handleImg('/static/index/img/wechat.png')
                ]
            ];
        }

        if(isset($result))
        {
            return Result::success('获取成功',$result);
        }else{
            return Result::fail('获取失败');
        }
    }


    /**
     * post: 付款
     * path: confirmPayment
     * method: confirmPayment
     * param: pay_way - {string} 支付方式
     * param: a_id - {string} 订单id
     * param: pay_password  密码
     */
    public function confirmPayment()
    {
        $param = $this->request->post();
        if($param['pay_way']!='wechat')
        {
            if (md5($param['pay_password']) != $this->user->pay_password) {
                return  Result::fail('支付密码错误');
            }
        }
        $order = ActivityOrderModel::confirmPayment($param, $this->user);
        if($order['code']>0)
        {
            return Result::success($order['msg'],$order['data']);
        }else{
            return Result::fail($order['msg']);
        }
    }


    /**
     * get: 订单列表
     * path: getList
     * method: getList
     * param: page {string} 页数
     * param: type {string} 全部，1待付款，2待发货,3已完成
     */
    public function getList()
    {
        $param = $this->request->param();
        if (isset($param['type']) && $param['type']) {
         $map['status'] = $param['type'];
        }
        $map['uid'] = $this->user->id;
        $map['is_del'] = 0;

        $List = ActivityOrderModel::getPageList($map, 'id,sn,pay_way,pay_state,pay_time,status,create_time,activity_name,activity_image,total_amount,real_amount,receiver,receiver_tel,address_details', $param['page'], 20, false, []);
        foreach ($List['data'] as $k=>$v){
            $v->status = $v->is_status;
            $v->pay_way = $v->pay_ways;
        }
        if(isset($List))
        {
            return Result::success('获取成功',$List);
        }else{
            return Result::fail('获取失败');
        }
    }


    /**
     * get: 订单详情
     * path: my_order_details
     * method: my_order_details
     * param: id {string} 订单id
     */
    public function my_order_details()
    {
        $id = $this->request->param('id');
        $info = ActivityOrderModel::where(['uid' => $this->user->id, 'id' => $id])->field('id,sn,pay_way,pay_state,pay_time,status,create_time,activity_name,activity_image,total_amount,real_amount,receiver,receiver_tel,address_details')->find();
        if(!$info){
            return Result::fail('订单不存在');
        }
        $info['product'] = OrderProduct::where(['order_id' => $info->id])->select();
        $info->status = $info->is_status;
//        $info->addressData = \app\common\model\Address::find($info->address_id);
//            [
//            'receiver' => $info->address->nickname,
//            'receiver_tel' => $info->address->tel,
//            'province' => $info->address->p->title,
//            'city' => $info->address->c->title,
//            'district' => $info->address->d->title,
//            'details' => $info->address->details
//        ];
        if(isset($info))
        {
            return Result::success('获取成功',$info);
        }else{
            return Result::fail('获取失败');
        }
    }


    /**
     * get: 商品评价页
     * path: getOrderProduct
     * method: getOrderProduct
     * param: id
     */
    public function getOrderProduct(): \think\Response
    {
        $param = $this->request->post();
        $orderProduct = OrderProduct::where('order_id',$param['id'])->field('id,order_id,pid,product_title,product_image')->select();
        return Result::success('获取成功',$orderProduct);
    }

    /**
     * post: 评价商品
     * path: addEvaluatet
     * method: addEvaluatet
     * param: order_id - {string} 订单id
     * param: list - {string} pronum评分 images图片 pid商品id content评价内容
     */
    public function addEvaluatet(){
        $param = $this->request->post();
        try {
            //$this->validate($param, 'Evaluate');
            return Result::fastReturn(Evaluate::addEvaluatet($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }
    }




    /**
     * post: 申请退款
     * path: drawback
     * method: drawback
     * param: id {string} 订单id
     */
    public function drawback()
    {
        $id = $this->request->param('id');
        $info = OrderModel::where(['uid' => $this->user->id, 'id' => $id])->field('id,real_amount')->find();
        if(isset($info))
        {
            return Result::success('获取成功',$info);
        }else{
            return Result::fail('获取失败');
        }

    }

    /**
     * post: 申请提交
     * path: drawback_save
     * method: drawback_save
     * param: id {string} 订单id
     * param: refund_explain {string} 退款说明
     * param: refund_imgs {string} 退款凭证
     */
    public function drawback_save()
    {
        $param = $this->request->param();
        $order = OrderModel::find($param['id']);
        if($order->is_refund == 1)
        {
            return Result::fail('已申请过退款');
        }
        try {
            $this->validate($param, 'Order');
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }
        $data = OrderModel::update(['refund_explain'=>$param['refund_explain'],'p_refund_time'=>time(),'refund_imgs'=>$param['refund_imgs'],'is_refund'=>1,'status'=>3],['id'=>$param['id']]);
        if($data)
        {
            return Result::success('申请退款成功');
        }else{
            return Result::fail('申请退款失败');
        }
    }


    /**
     * post: 申请提交
     * path: edit
     * method: edit
     * param: id {string} 订单id
     * param: type {string} clear取消
     */
    public function edit(): \think\Response
    {
        $param = $this->request->post();
        $result = OrderModel::edit($param, $this->user);
        if($result>0)
        {
            return Result::success($result['msg']);
        }else{
            return Result::fail($result['msg']);
        }
    }

}


